package com.fingard.dsp.bank.scheduler;

import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;

import com.fingard.FGBiz;
import com.fingard.constant.Format;
import com.fingard.dsp.bank.directConfig.TaskTimerSet;
import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.dsp.bank.directbank.bankInterface.IScheduleTask;
import com.fingard.dsp.bank.reqrespobj.ReqRespHeader;
import com.fingard.text.StringHelper;
import com.fingard.util.DateHelper;

public class TaskExecutor {

    private static AtomicInteger integerID = new AtomicInteger(0);

    public void execTask(TaskTimerSet p_taskSet) {
    	String logCode = "execTask";
        try {
            if (p_taskSet != null) {
                StringBuilder sbLog = new StringBuilder();

                int tmpIntID = integerID.incrementAndGet();
                if (tmpIntID >= Integer.MAX_VALUE - 100) {
                    integerID = new AtomicInteger(0);
                    tmpIntID = integerID.incrementAndGet();
                }
                if(StringHelper.hasAnyChar(p_taskSet.bankFront.bankCode)){
                	logCode = p_taskSet.bankFront.bankCode;
                }
                Date timeStart = new Date();
                //sbLog.append(Format.DateTimeSSSFormat.format(timeNow) + " " + tmpIntID + " 开始执行" + p_taskSet.bankFront.bankCode + "启动前置机" + p_taskSet.bankFront.code + "-" + p_taskSet.bankFront.name + "的定时任务\r\n");
                ReqRespHeader tmpHeader = new ReqRespHeader();
                tmpHeader = new ReqRespHeader();
                tmpHeader.bankCode = p_taskSet.bankFront.bankCode;
                tmpHeader.corporation = p_taskSet.bankFront.corporation;
                tmpHeader.group = p_taskSet.bankFront.group;
                tmpHeader.transType = "ScheduleTask."+p_taskSet.taskNameList;
                tmpHeader.transTime = Format.DateTimeFormat.format(timeStart);
                FGBiz.logTrace.infoNoTimeBank(logCode, tmpHeader.transType, Format.DateTimeSSSFormat.format(timeStart) + " " + tmpIntID + " 开始执行" + p_taskSet.bankFront.bankCode + "启动前置机" + p_taskSet.bankFront.code + "-" + p_taskSet.bankFront.name + "的定时任务");
                sbLog.append(tmpIntID+"\r\n");
                try {
                    String startJAVAversionDate = FGBiz.dspCenter.dspConfig.getValueAsString("startJAVAversionDate");
                    if(StringHelper.hasAnyChar(startJAVAversionDate) && p_taskSet.taskNameList.toLowerCase().contains("downreceiptpro")){
                        if(DateHelper.compareDay(timeStart,Format.Date8Format.parse(startJAVAversionDate))<0){
                            sbLog.append(Format.DateTimeSSSFormat.format(timeStart) + " JAVA版本上线日期不能晚于当前定时任务执行日期");
                            FGBiz.logTrace.infoNoTimeBank(logCode, tmpHeader.transType, sbLog.toString());
                            return;
                        }
                    }
                    String className = "com.fingard.dsp.bank.directbank." + p_taskSet.bankFront.bankCode.toLowerCase() + "." + p_taskSet.bankFront.bankCode + "ScheduleTask";
                    Class<?> bankClass = Class.forName(className);
                    if (bankClass != null) {

                        DirectBase tmpDirectBase = (DirectBase) bankClass.newInstance();
                        tmpDirectBase.reqHeader = tmpHeader;
                        tmpDirectBase.bankFront = p_taskSet.bankFront;
                        tmpDirectBase.setSbLog(sbLog);
                        tmpDirectBase.setLogFlushTime(timeStart);

                        IScheduleTask iTask = (IScheduleTask) tmpDirectBase;
                        iTask.execTask(p_taskSet);
                    }
                } catch (Throwable t) {
                	FGBiz.logTrace.exception(logCode, t, tmpIntID+"异常");
                }
                Date timeEnd = new Date();
                double secsTaken = DateHelper.getDifferSecs(timeEnd, timeStart);
                sbLog.append(Format.DateTimeSSSFormat.format(timeEnd) + " " + tmpIntID + " 执行结束，耗时："+Format.DecimalAmtFormat.format(secsTaken)+"秒\r\n");
                //LogHelper.writeBankLogByHour(timeNow, "", tmpHeader, sbLog);
                FGBiz.logTrace.infoNoTimeBank(logCode, tmpHeader.transType, sbLog.toString());
            }
        } catch (Exception ex) {
        	FGBiz.logTrace.exception(logCode, ex);
            
        } catch (Throwable t2) {
        	FGBiz.logTrace.exception(logCode, t2);
        }
    }
}
